package cn.ctvonline.android.kaopu.base.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import cn.ctvonline.android.kaopu.R;

/**
 * Created by dujian on 15/11/25.
 */
public class WindowUtil {
    static int screenWidth = -1;
    static int screenHeight = -1;

    public static int getScreenWidth(Context context) {
        if (screenWidth == -1) {
            WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Point point = new Point();
            m.getDefaultDisplay().getSize(point);
            screenWidth = point.x;
            screenHeight = point.y;
        }
        return screenWidth;
    }

    public static int getScreenHeight(Context context) {
        if (screenHeight == -1) {
            WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Point point = new Point();
            m.getDefaultDisplay().getSize(point);
            screenWidth = point.x;
            screenHeight = point.y;
        }
        return screenHeight;
    }

    public static void translucentStatusBar(Activity activity, boolean on) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (activity != null) {
                int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                Window window = activity.getWindow();
                WindowManager.LayoutParams winParams = window.getAttributes();
                if (on) {
                    winParams.flags |= flags;
                } else {
                    winParams.flags &= ~flags;
                }
            }
        }
    }

    public static void translucentNavigation(Activity activity, boolean on) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (activity != null) {
                int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
                Window window = activity.getWindow();
                WindowManager.LayoutParams winParams = window.getAttributes();
                if (on) {
                    winParams.flags |= flags;
                } else {
                    winParams.flags &= ~flags;
                }
            }
        }
    }

    @TargetApi(19)
    public static void setTranslucentStatus(Activity activity, boolean on) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            Window win = activity.getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            if (on) {
                winParams.flags |= bits;
            } else {
                winParams.flags &= ~bits;
            }
            win.setAttributes(winParams);
            SystemBarTintManager tintManager = new SystemBarTintManager(activity);
            tintManager.setStatusBarTintEnabled(on);
            tintManager.setNavigationBarTintEnabled(true);
            tintManager.setStatusBarTintColor(activity.getResources().getColor(R.color.colorPrimaryDark));
            tintManager.setNavigationBarTintColor(activity.getResources().getColor(R.color.colorPrimaryDark));
            tintManager.setStatusBarTintResource(R.color.colorPrimaryDark);
        }
    }
}
